1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.UI;
5
6 public
class Structure : MonoBehaviour {
7
8     
public int maxHitPoints;
9     
public Sprite[] sprite;
10     
public float damageCounter;
11     
public GameObject disassemble;
12     
public AudioClip stoneSound, woodSound, glassSound;
13     
public enum StructureType{
14         Wood,
15         Stone,
16         Glass
17     }
18
19     
public StructureType structure;
20
21     
public int hitpoints;
22     
private SpriteRenderer spriteRenderer;
23     
private Vector3 bounce;
24     
private int maxScore;
25     
private int counter;
26
27     
void Awake(){
28         spriteRenderer = GetComponent<SpriteRenderer> ();
29     }
30
31     
// Use this for initialization
32     
void Start () {
33         InitializeVariables ();
34     }
35     
36     
// Update is called once per frame
37     
void Update () {
38     }
39
40     
void InitializeVariables(){
41         hitpoints = maxHitPoints;
42         maxScore =
500;
43         counter =
2;
44     }
45
46     
void OnCollisionEnter2D(Collision2D collision){
47         
if(collision.relativeVelocity.magnitude > damageCounter){
48             hitpoints -= Mathf.RoundToInt (collision.relativeVelocity.magnitude);
49             UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
50         }
51
52
53         
if (hitpoints <= 50) {
54             spriteRenderer.sprite = sprite [
0];
55             
if(counter == 2){
56                 AudioManager ();
57                 counter--;
58             }
59
60         }
61
62         
if(hitpoints <= 30){
63             spriteRenderer.sprite = sprite [
1];
64             
if(counter == 1){
65                 AudioManager ();
66                 counter--;
67             }
68         }
69
70
71         
if(hitpoints <= 0){
72             Destroyed ();
73
74             
if(collision.gameObject.CompareTag("Player Bullet")){
75                 bounce = collision.transform.GetComponent<Rigidbody2D> ().velocity;
76                 bounce.y =
0f;
77                 collision.transform.GetComponent<Rigidbody2D> ().velocity = bounce;
78             }
79         }
80     }
81         
82     
void Destroyed(){
83         Destroy (gameObject);
84         GameObject newDisassemble = Instantiate (disassemble, transform.position, Quaternion.identity)
as GameObject;
85         Destroy (newDisassemble,
3f);
86         
if(GameController.instance != null){
87             GameController.instance.score += maxScore;
88         }
89
90         GameObject scoreText = Instantiate (Resources.Load(
"Score Text Canvas"), new Vector3 (transform.position.x, transform.position.y + 1f, transform.position.z), Quaternion.identity) as GameObject;
91         scoreText.transform.GetChild (
0).transform.GetComponent<Text> ().text = maxScore.ToString();
92         Destroy (scoreText,
2f);
93     }
94
95     
void UpdateScoreStatus(int hitScore){
96         
if(GameController.instance != null){
97             GameController.instance.score += hitScore;
98         }
99     }
100
101     
void AudioManager(){
102         
switch (structure) {
103         
case StructureType.Wood:
104             
if(GameController.instance != null && MusicController.instance != null){
105                 
if(GameController.instance.isMusicOn){
106                     
if (gameObject != null) {
107                         AudioSource.PlayClipAtPoint (woodSound, transform.position);
108                     }
109                 }
110             }
111             
break;
112
113         
case StructureType.Stone:
114             
if(GameController.instance != null && MusicController.instance != null){
115                 
if(GameController.instance.isMusicOn){
116                     
if (gameObject != null) {
117                         AudioSource.PlayClipAtPoint (stoneSound, transform.position);
118                     }
119                 }
120             }
121             
break;
122
123         
case StructureType.Glass:
124             
if(GameController.instance != null && MusicController.instance != null){
125                 
if(GameController.instance.isMusicOn){
126                     
if (gameObject != null) {
127                         AudioSource.PlayClipAtPoint (glassSound, transform.position);
128                     }
129                 }
130             }
131             
break;
132         }
133     }
134 }


Use this for initialization

Update is called once per frame




trò chơi Cannon Siege miễn phí 12.312 lượt xem

Gõ tìm kiếm nhanh...